/*
 * 支付
 */
export const pay = (result, self, success, fail) => {
	console.log('=========开始支付pay.js=====')
	console.log(result)
	
	if (result.code === -10) {
		uni.showToast({
			title: result.msg,
			duration: 2000,
			icon: 'none',
		});
		return false;
	}

	// 发起微信支付
	if (result.data.pay_type == 20) {
		//小程序支付
		// #ifdef  MP-WEIXIN	
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: (result.data.timeStamp).toString(),
			nonceStr: result.data.nonceStr,
			package: result.data.package,
			signType: 'MD5',
			paySign: result.data.paySign,
			success: res => {
				paySuccess(result, self, success);
			},
			fail: res => {
				console.log(res)
				uni.showToast({
					title: '订单未支付成功',
					duration: 2000,
					icon: 'none',
					success: function(res) {
						payError(result, fail);
					}
				});
			},
		});
		// #endif
		//公众号支付
		// #ifdef  H5
		if (self.isWeixin()) {
			WeixinJSBridge.invoke('getBrandWCPayRequest', JSON.parse(result.data.payment),
				function(res) {
					if (res.err_msg == "get_brand_wcpay_request:ok") {
						paySuccess(result, self, success);
					} else if (res.err_msg == "get_brand_wcpay_request:cancel") {
						uni.showToast({
							title: '支付取消',
							duration: 2000,
							icon: 'none',
							success: function(res) {
								payError(result, fail);
							}
						});
					} else {
						uni.showToast({
							title: '订单未支付成功',
							duration: 2000,
							icon: 'none',
							success: function(res) {
								payError(result, fail);
							}
						});
					}
				}
			);
		} else {
			//h5支付
			uni.navigateTo({
				url: '/pages/order/pay-h5/pay-h5?order_id=' + result.data.order_id + '&order_type=' + result
					.data.order_type,
			});
			return;
		}
		// #endif
		// #ifdef  APP-PLUS
		//微信支付
		wxAppPay(result, self, success, fail);
		// #endif
	}

	// 余额支付
	if (result.data.pay_type == 10) {
		paySuccess(result, self, success);
	}

	// 支付宝支付
	if (result.data.pay_type == 30) {
		// #ifdef  APP-PLUS
		aliAppPay(result, self, success, fail);
		// #endif
		// #ifdef  H5
		uni.navigateTo({
			url: '/pages/order/alipay-h5/alipay-h5?order_id=' + result.data.order_id + '&order_type=' +
				result.data.order_type,
		});
		// #endif
	}
}

function wxAppPay(result, self, success, fail) {
	// 获取支付通道  
	//plus.payment.getChannels(function(channels) {

	var orderInfo = {
		"appid": result.data.appid,
		"noncestr": result.data.noncestr,
		"package": "Sign=WXPay",
		"partnerid": result.data.partnerid,
		"prepayid": result.data.prepayid,
		"timestamp": result.data.timestamp,
		"sign": result.data.sign,
	};
	console.log('发起支付信息===')
	console.log(orderInfo);
	uni.requestPayment({
		provider: 'wxpay',
		orderInfo: orderInfo,
		success(res) {
			paySuccess(result, self, success);
		},
		fail(error) {
			console.log(error);
			uni.showToast({
				title: '订单未支付成功',
				duration: 2000,
				icon: 'none',
				success: function(res) {
					payError(result, fail);
				}
			});
		},
	});
	// }, function(e) {
	// 	console.log("获取支付通道失败：" + e.message);
	// });
}

function aliAppPay(result, self, success, fail) {
	console.log('支付宝--配置信息');
	console.log(result.data.aliPay);
	uni.requestPayment({
		provider: 'alipay',
		orderInfo: result.data.aliPay,
		success(res) {
			console.log('res');
			console.log(res);
			paySuccess(result, self, success);
		},
		fail(error) {
			console.log('error');
			console.log(error);
			uni.showToast({
				title: '订单未支付成功',
				duration: 2000,
				icon: 'none',
				success: function(res) {
					payError(result, fail);
				}
			});
		},
	});
}


// 跳到支付成功页
function paySuccess(result, self, success) {
	if (success) {
		success(result);
		return;
	}
	gotoSuccess(result);
}

// 跳到支付成功页
function gotoSuccess(result) {
	uni.navigateTo({
		url: '/pages/order/orderList/orderList'
	});
}

// 支付失败跳订单详情
function payError(result, fail) {
	if (fail) {
		fail(result);
		return;
	}
	uni.navigateTo({
		url: '/pages/order/orderList/orderList'
	});
}